<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
    var typeid = 0;
    $('.select-typeid').click(function () {
        typeid = $(this).val();
        $("#dataTables").dataTable().fnDraw(false);
    });
    var tables = $('#dataTables').DataTable({
        "sAjaxSource": "{{ urlfor "StocksController.Create"}}"
        , "bLengthChange": false
        , "fnServerData": function (sSource, aoData, fnCallback) {
            if (typeid == 0) {
                fnCallback({'aaData': [], 'iTotalDisplayRecords': 0});
                return false;
            }
            var queryParams = checkQueryTableData(aoData);
            queryParams.Typeid = Number(typeid);
            queryParams.Filter = $('#filter').val();
            $.post(sSource, JSON.stringify(queryParams), function (resp) {
                fnCallback(resp);
            }, 'json')
        }
        , columns: [
            {"data": "BrandName", "bSortable": false}
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return productGroupName(full);
                }
            }
            , {
                "mDataProp": "Action",
                "bSortable": false,
                "sWidth": "15%",
                "render": function (data, type, full, meta) {
                    btns = '<a href="javascript:;" class="am-text-danger" onclick="save(' + full.Id + ', \'' + full.Name + '\');return false;">指定该商品盘点</a>';
                    return btns;
                }
            }
        ]
        , "aLengthMenu": [20, 50, 100, 200]
		,'bLengthChange': true
        , "serverSide": true
        , "bPaginate": true
        , "bFilter": false	//是否启动过滤、搜索功能
        , "bInfo": true //是否显示页脚信息，DataTables插件左下角显示记录数
        , "bProcessing": true //DataTables载入数据时，是否显示‘进度’提示
    });
    $('#filter').blur(function(){
        $("#dataTables").dataTable().fnDraw(false);
    });
    function save(id, name) {
        layer.confirm('指定商品' + name + ',并生成盘点表?', function (index) {
            $.post('{{ urlfor "StocksController.Save" }}', {'products_id': id, 'typeid': typeid}, function (res) {
               if(res.code == 1) {
                   layer.alert(res.msg);return false;
               }
               location = '{{ urlfor "StocksController.SaveList" }}/'+res.data;
            });
            layer.close(index);
        });
    }
</script>